;(function (global,outputName, factory) {
	typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
	typeof define === 'function' && define.amd ? define(factory) :
	(global[outputName] = factory());
}(this,'WsjPreviewImg', function () {
    var WsjPreviewImg = function (cls) { 
        cls=cls || 'wsj_touch_wrap'
        arr = [].slice.call(document.getElementsByClassName(cls))
        arr.forEach(function (v) {
            //[data-wsjaddedflagpreviewimg2019]这个名字可以随便写,只是为了做标记而已
            if (!v.hasAttribute('data-wsjaddedflagpreviewimg2019')) {
                v.setAttribute('data-wsjaddedflagpreviewimg2019', '1')
                v.addEventListener('click', function (e) {
                    var _touchimg = e.target.dataset.touchimg
                    if (e.target.tagName.toLowerCase() === 'img' && e.target.dataset.touchimg) {
                        /*
                         * 本来有这个正则:/\.(jpeg|jpg|png|gif|webp)$/i.test(_touchimg)
                         * 但是考虑到有些图片地址不是上述后缀,所以就去掉了上述判断条件
                        */
                        wsjTouchImg(_touchimg)
                    }
                }, false)
            }
        })
    
        function wsjTouchImg(imgSrc) {
            var docHeight = document.documentElement.clientHeight
            var docWidth = document.documentElement.clientWidth
            var origin = {
                x1: 0,
                y1: 0,
                x2: 0,
                y2: 0
            }
    
            var timer
            var bPrepareDbl = false
            var maxScale = 2
            var minScale = 0.8
            var orginDistance = 0
            var originScale = 1
            var sumScale = 1
            var bBeginSingle = false
            var originSingleX = 0
            var originSingleY = 0
            var singleSumTransformX = 0
            var singleSumTransformY = 0
            var absLeftRight
            var minLeftRight
            var absTopBottom
            var minTopBottom
            var ELE
    
            var oMask = document.createElement('div')
            oMask.className = "wsj-touch-mask"
            oMask.id = "wsj_touch_mask"
    
            var oLoad = document.createElement('div')
            oLoad.className = "wsj-touch-loading-icon"
    
            var oImg = document.createElement('img')
            oImg.className = "wsj-touch-pop-img"
            oImg.id = 'wsj_touch_img'
            oImg.style.display = 'none'
            oImg.onload = function () {
                oImg.style.display = 'block'
                oLoad.style.display = 'none'
                var _wRatio = oImg.width / docWidth
                var _hRatio = oImg.height / docHeight
                if (_wRatio > 2 || _hRatio > 2) {
                    maxScale = Math.max.call(null, _wRatio, _hRatio)
                }
            }
            oImg.onerror = function () {
                oMask.innerHTML = '<span style="color:#fff;font-size:20px;">图片不存在</span>'
            }
            oImg.src = imgSrc
    
            oMask.appendChild(oLoad)
            oMask.appendChild(oImg)
            document.body.appendChild(oMask)
    
            ELE = document.getElementById('wsj_touch_img')
    
    
            ELE.addEventListener('touchstart', handleTouchStart, false)
            ELE.addEventListener('touchmove', handleTouchMove, false)
            ELE.addEventListener('touchend', handleTouchEnd, false)
            ELE.addEventListener('click', handleClick, false)
            oMask.addEventListener('click', handleMask, false)
    
    
            function handleTouchStart(e) {
                ELE.classList.remove('wsj-touch-transition')
                if (e.targetTouches.length === 2) {
                    bBeginSingle = false
                    origin.x1 = e.targetTouches[0].clientX
                    origin.y1 = e.targetTouches[0].clientY
                    origin.x2 = e.targetTouches[1].clientX
                    origin.y2 = e.targetTouches[1].clientY
                    orginDistance = GetDistance(origin.x1, origin.y1, origin.x2, origin.y2)
                }
                if (e.targetTouches.length === 1) {
                    bBeginSingle = true
                    origin.x1 = e.targetTouches[0].clientX
                    origin.y1 = e.targetTouches[0].clientY
                }
            }
    
            function handleTouchMove(e) {
                e.preventDefault()
                if (e.targetTouches.length === 2) {
                    x3 = e.targetTouches[0].clientX
                    y3 = e.targetTouches[0].clientY
                    x4 = e.targetTouches[1].clientX
                    y4 = e.targetTouches[1].clientY
                    sumScale = originScale * GetDistance(x3, y3, x4, y4) / orginDistance
                    ELE.style.transform = "translate3d(" + singleSumTransformX + "px," + singleSumTransformY + "px,0) scale(" + sumScale + ")"
                }
                if (bBeginSingle && e.targetTouches.length === 1) {
                    x3 = e.targetTouches[0].clientX
                    y3 = e.targetTouches[0].clientY
    
                    if (x3 > origin.x1) {
                        singleSumTransformX = originSingleX - 0 + GetDistance(x3, 0, origin.x1, 0)
                    } else {
                        singleSumTransformX = originSingleX - GetDistance(x3, 0, origin.x1, 0)
                    }
    
                    if (y3 > origin.y1) {
                        singleSumTransformY = originSingleY - 0 + GetDistance(0, y3, 0, origin.y1)
                    } else {
                        singleSumTransformY = originSingleY - GetDistance(0, y3, 0, origin.y1)
                    }
    
                    ELE.style.transform = "translate3d(" + singleSumTransformX + "px," + singleSumTransformY + "px,0) scale(" + sumScale + ")"
                }
    
            }
            function handleTouchEnd(e) {
                if (e.targetTouches.length == 0) {
                    if (sumScale > maxScale) {
                        sumScale = maxScale
                    } else if (sumScale < minScale) {
                        sumScale = minScale
                    }
                    ELE.classList.add('wsj-touch-transition')
                    //处理宽度
                    absLeftRight = Math.abs(ELE.width * sumScale - docWidth) / 2
                    if (ELE.width * sumScale >= docWidth) {
                        if (singleSumTransformX >= absLeftRight) {
                            singleSumTransformX = absLeftRight
                        }
                        if (singleSumTransformX <= (-1 * absLeftRight)) {
                            singleSumTransformX = -1 * absLeftRight
                        }
                    } else {
                        if (singleSumTransformX >= absLeftRight) {
                            singleSumTransformX = absLeftRight
                            ELE.style.transform = "translate3d(" + singleSumTransformX + "px," + singleSumTransformY + "px,0) scale(" + sumScale + ")"
                        }
                        if (singleSumTransformX <= (-1 * absLeftRight)) {
                            singleSumTransformX = -1 * absLeftRight
                        }
                    }
    
                    //处理高度
                    absTopBottom = Math.abs(ELE.height * sumScale - docHeight) / 2
                    if (ELE.height * sumScale >= docHeight) {
                        if (singleSumTransformY >= absTopBottom) {
                            singleSumTransformY = absTopBottom
                        }
                        if (singleSumTransformY <= (-1 * absTopBottom)) {
                            singleSumTransformY = -1 * absTopBottom
                        }
                    } else {
                        if (singleSumTransformY >= absTopBottom) {
                            singleSumTransformY = absTopBottom
                        }
                        if (singleSumTransformY <= (-1 * absTopBottom)) {
                            singleSumTransformY = -1 * absTopBottom
                        }
                    }
    
                    ELE.style.transform = "translate3d(" + singleSumTransformX + "px," + singleSumTransformY + "px,0) scale(" + sumScale + ")"
                    originScale = sumScale
                    originSingleX = singleSumTransformX
                    originSingleY = singleSumTransformY
                }
            }
    
    
            function handleClick() {
                if (bPrepareDbl) {
                    clearTimeout(timer)
                    if (sumScale >= maxScale) {
                        sumScale = 1
                        singleSumTransformX = 0
                        singleSumTransformY = 0
                    } else {
                        sumScale = maxScale
                    }
                    ELE.classList.add('wsj-touch-transition')
                    ELE.style.transform = "translate3d(" + singleSumTransformX + "px," + singleSumTransformY + "px,0) scale(" + sumScale + ")"
                    originScale = sumScale
                    originSingleX = singleSumTransformX
                    originSingleY = singleSumTransformY
                    bPrepareDbl = false
                    return
                }
                bPrepareDbl = true
    
                timer = setTimeout(function () {
                    bPrepareDbl = false
                    if (sumScale !== 1 || originSingleX !== 0 || originSingleY !== 0) {
                        originScale = sumScale = 1
                        originSingleX = singleSumTransformX = 0
                        originSingleY = singleSumTransformY = 0
                        ELE.style.transform = "translate3d(" + singleSumTransformX + "px," + singleSumTransformY + "px,0) scale(" + sumScale + ")"
                    } else {
                        oMask.remove()
                    }
                }, 200)
            }
    
            function handleMask(e) {
                e.stopPropagation()
                if (e.target.tagName.toLowerCase() === 'div') {
                    ELE.removeEventListener('touchstart', handleTouchStart)
                    ELE.removeEventListener('touchmove', handleTouchMove)
                    ELE.removeEventListener('touchend', handleTouchEnd)
                    oMask.removeEventListener('click', handleMask)
                    this.remove()
                }
            };
    
            //获取坐标轴上两点之间的距离
            function GetDistance(x1, y1, x2, y2) {
                return Math.sqrt(Math.pow(x1 - x2, 2) + Math.pow(y1 - y2, 2));
            };
        }
    
    }
    return WsjPreviewImg
 }));